

import {getFocusList} from "../../../api/um";

const {eventBus, config, colorOption} = getApp().globalData;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    colorOption,
    // 分页参数
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      totalRow: 0,
      attrQueryMap: {}
    },
    // 资讯列表
    focusList: [],
    // 显示加载中
    isLoading: true,
    type:null,
    titleMap:{
      followers:'关注我的',
      following:'我关注的',
      mutual:'相互关注'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({type:options.type})
    this.getFocusList(options.type)
  },

  onUnload() {
  },

  getFocusList(type) {
    wx.showLoading({title: '加载中',})
    this.setData({isLoading: true})
    getFocusList({
      data: {
        ...this.data.queryParams,
        type:type||this.data.type
      },
      callBack: (res) => {
        let focusList = this.data.queryParams.pageNum === 1 ? [] : this.data.focusList
        this.setData({
          'queryParams.totalRow': res.total,
          isLoading: false,
          focusList: focusList.concat(res.rows.map(item => {
            item.attrDataMap.TX = item.attrDataMap.TX ? config.localImgSrc + item.attrDataMap.TX : (item.attrDataMap.XB == 1 ? '/images/mine/defaultAvatar_male.png' : '/images/mine/defaultAvatar_female.png');
            return item;
          }))
        })
        setTimeout(()=>wx.hideLoading(), 200);
        wx.stopPullDownRefresh();
      },
    });
  },
    // 关注按钮
    handleFocus(e) {
      let {id} = e.detail.currentTarget.dataset
      let focusList = this.data.focusList;
      focusList.forEach(item=>{
        if(item.id === id){
          item.isFocus = !item.isFocus;
        }
      })
      this.setData({focusList})
    },
  goDetail(e){
    const {item} = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pageA/pages/contentDetail/index?id=${item.id}&title=${this.data.showData.text}`,
    })
  },
  //  搜索
  onSearch() {
    this.setData({
      'queryParams.totalRow': 0,
      'queryParams.pageNum': 1,
      focusList: [],
      isLoading: true,
    })
    this.getFocusList()
  },
  onChange(e) {
    this.setData({
      'queryParams.key': e.detail,
    });
  },
  goBack() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      'queryParams.totalRow': 0,
      'queryParams.pageNum': 1,
      focusList: [],
      isLoading: true,
    })
    this.getFocusList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("上拉触底");
    if (this.data.queryParams.totalRow > this.data.focusList.length) {
      this.data.queryParams.pageNum++
      this.getFocusList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  },
});
